package cn.edu.swu.bc.servlet;

import cn.edu.swu.bc.filter.AuthFilter;
import cn.edu.swu.bc.utils.JsonUtils;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.Writer;

public class GetHeadInfoServlet extends HttpServlet {

    private static final long serialVersionUID = 119812548834340L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session=request.getSession();
        //读取session中的信息
        String nickname=(String) session.getAttribute(AuthFilter.USER_NICKNAME);
        String avatar=(String) session.getAttribute(AuthFilter.USER_AVATAR);
        String signature=(String) session.getAttribute(AuthFilter.USER_SIGNATURE);
        //设置返回数据格式和字符集
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        Writer writer = response.getWriter();
        //将读取的信息打包成json格式
        writer.write(JsonUtils.headInfoToJson(nickname,avatar,signature));
    }

}